//
//  GKZYMyInfoSettingViewController.m
//  xxptmobile
//
//  Created by KwokChun Cheung on 15/11/5.
//  Copyright © 2015年 gzedu. All rights reserved.
//

#import "GKZYMyInfoSettingViewController.h"
#import "GKZYMyInfoFeedbackViewController.h"
#import "GKZYLoginViewController.h"
#import "GKZYMyInfoModel.h"

@interface GKZYMyInfoSettingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *mainTableView;
@property (nonatomic,strong) NSMutableArray *datas;
@property (nonatomic,strong) NSMutableArray *cells;
@end

@implementation GKZYMyInfoSettingViewController

-(instancetype)init{
    if (self = [super init]) {
        self.cells = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设置";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:238/255.f green:238/255.f blue:238/255.f alpha:1];
    
    [self.view addSubview:self.mainTableView];
    [self fillData];
}


-(void)fillData{
    
    self.datas = [[NSMutableArray alloc]init];
    
    NSMutableArray *rows1 = [[NSMutableArray alloc]init];

    GKZYMyInfoModel *model15 = [GKZYMyInfoModel new];
    model15.title = @"意见反馈";
    model15.forwardToFeedback = YES;
    model15.showArrows = YES;
    [rows1 addObject:model15];
    
    [self.datas addObject:rows1];
    
    
    NSMutableArray *rows2 = [[NSMutableArray alloc]init];
    GKZYMyInfoModel *model21 = [GKZYMyInfoModel new];
    model21.title = @"注销登录";
    model21.forwardToLogout = YES;
    [rows2 addObject:model21];
    
    [self.datas addObject:rows2];
    
    
}


#pragma mark - tableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSMutableArray *rows = self.datas[section];
    return rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellName = @"myInfoSetting";
    GKZYMyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell){
        NSMutableArray *rows = self.datas[indexPath.section];
        GKZYMyInfoModel *model = rows[indexPath.row];
        
        cell = [[GKZYMyInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.hiddenTopLine = indexPath.row != 0 && !model.isHeader;
        cell.data = model;
        
        [self.cells addObject:cell];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *rows = self.datas[indexPath.section];
    GKZYMyInfoModel *model = rows[indexPath.row];
    if (model.isHeader) {
        return 70;
    }else{
        return 50;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 19;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSMutableArray *rows = self.datas[indexPath.section];
    GKZYMyInfoModel *model = rows[indexPath.row];
    
    UIViewController *vc;
    if (model.forwardToFeedback){
        vc = [[GKZYMyInfoFeedbackViewController alloc]init];
    }else if (model.forwardToLogout){
        vc = [[GKZYLoginViewController alloc] initOnLogout];
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    
    if (vc) {
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark - Getter For Views

-(UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = ({
            UITableView *myInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64-40) style:UITableViewStyleGrouped];
            myInfoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            myInfoTableView.dataSource = self;
            myInfoTableView.delegate = self;
            myInfoTableView.scrollEnabled = NO;
            myInfoTableView;
        });
    }
    return _mainTableView;
}

@end
